import { Request, Response, NextFunction } from "express";
import jwt from "jsonwebtoken";
import { jwtSecret } from "@/configs/jwt";

export const authVerifier = (request: Request, response: Response, next: NextFunction) => {
	try {
		const token = request.headers.authorization?.split(" ")[1] || "";
		jwt.verify(token, jwtSecret, (err, decoded) => {
			if (err) {
				response.statusCode = 401;
				throw new Error("Token无效");
			}
			response.locals.user = decoded;
			next();
		});
	} catch (err: any) {
		next(err);
	}
};
